package org.dtrd.modules.evaluation.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.constant.Evaluation;
import org.dtrd.modules.evaluation.entity.bean.HappyInfo;
import org.dtrd.modules.evaluation.entity.bean.JobInfo;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdHappy;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdJob;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdHappyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Optional;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_ADD;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * @Author QR
 * @Date 2021/11/1 1:56 PM
 */
@Api(tags = "量表评估")
@RestController
public class HappyController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdHappyService happyService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("添加总体幸福感评估")
    @PostMapping(value = {"/105/45/1054501", "/100/17/1001717"})
    public Result<?> saveHappyInfo(@RequestBody HappyInfo happyInfo){
        if (happyInfo.getInput() == null
                || happyInfo.getInput().isEmpty()) {
            return error(STATE_PARAMS_MISSING);
        }
        boolean inValidInput = happyInfo.getInput().size() != Evaluation.ProblemNum.HAPPY.getNum();
        if (inValidInput){
            return error(STATE_PARAMS_ERROR);
        }
        Optional<Integer> any = happyInfo.getInput().stream()
                .filter(answer -> answer == -1)
                .findAny();
        if (any.isPresent()){
            return error(STATE_PARAMS_ERROR);
        }
        HappyInfo info = happyService.saveHappyInfo(happyInfo);
        String logContent = String.format("添加总体幸福感评估，添加用户id：%d", happyInfo.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_ADD);
        return Result.OK(info);
    }

    @ApiOperation("查询总体幸福感评估")
    @GetMapping("/105/45/1054502")
    public Result<?> getHappyInfoLatest(@PathParam("patientId") Integer patientId){
        if (isIntParamInvalid(patientId)) {
            return error(STATE_PARAMS_MISSING);
        }
        HappyInfo happyInfo = happyService.getHappyInfoLatest(patientId);
        String logContent = String.format("查询总体幸福感评估，查询用户id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(happyInfo);
    }

    @ApiOperation("查询总体幸福感评估列表")
    @GetMapping("/105/45/1054503")
    public Result<?> getJobList(
            @ApiParam("患者Id") @RequestParam("patientId") Integer patientId
    ){
        LambdaQueryWrapper<DtrdEntEvaluationRdHappy> queryWrapper = Wrappers.lambdaQuery(DtrdEntEvaluationRdHappy.class)
                .eq(DtrdEntEvaluationRdHappy::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdHappy::getCreateTime);
        List<DtrdEntEvaluationRdHappy> list = happyService.list(queryWrapper);
        return CollectionUtil.isNotEmpty(list) ? Result.OK(list.stream().map(po -> new HappyInfo().parseFromPo(po))) : error(STATE_NO_RECORD);
    }
}
